<!--

/*
** Copyright (c) 2012 The Khronos Group Inc.
**
** Permission is hereby granted, free of charge, to any person obtaining a
** copy of this software and/or associated documentation files (the
** "Materials"), to deal in the Materials without restriction, including
** without limitation the rights to use, copy, modify, merge, publish,
** distribute, sublicense, and/or sell copies of the Materials, and to
** permit persons to whom the Materials are furnished to do so, subject to
** the following conditions:
**
** The above copyright notice and this permission notice shall be included
** in all copies or substantial portions of the Materials.
**
** THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
** MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
*/

-->

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>WebGL texture2D GLSL conformance test.</title>
<link rel="stylesheet" href="../../../resources/js-test-style.css"/>
<link rel="stylesheet" href="../../../resources/glsl-feature-tests.css"/>
<script src="../../../js/js-test-pre.js"></script>
<script src="../../../js/webgl-test-utils.js"> </script>
</head>
<body>
<canvas id="example" width="32" height="32"></canvas>
<div id="description"></div>
<div id="console"></div>
<script id="vshader0" type="x-shader/x-vertex">
attribute vec4 vPosition;
attribute vec2 texCoord0;
varying vec2 texCoord;
void main() {
    gl_Position = vPosition;
    texCoord = texCoord0;
}
</script>
<script id="fshader0" type="x-shader/x-fragment">
precision mediump float;
uniform sampler2D tex;
uniform float divisor;
varying vec2 texCoord;
void main() {
    gl_FragData[0] = texture2DProj(tex, vec3(texCoord, divisor));
}
</script>
<script id="vshader1" type="x-shader/x-vertex">
attribute vec4 vPosition;
attribute vec2 texCoord0;
varying vec2 texCoord;
void main() {
    gl_Position = vPosition;
    texCoord = texCoord0;
}
</script>
<script id="fshader1" type="x-shader/x-fragment">
precision mediump float;
uniform sampler2D tex;
uniform float divisor;
varying vec2 texCoord;
void main() {
    gl_FragData[0] = texture2DProj(tex, vec4(texCoord, 123.0, divisor));
}
</script>
<script>
"use strict";
description("tests GLSL texture2DProj function with");

var wtu = WebGLTestUtils;
var gl = wtu.create3DContext("example", {antialias: false});

wtu.setupUnitQuad(gl, 0, 1);
var tex = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D, tex);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.REPEAT);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.REPEAT);

var c = document.createElement("canvas");
c.width = 16;
c.height = 16;
var ctx = c.getContext("2d");
ctx.fillStyle = "rgb(0,255,0)";
ctx.fillRect(0, 0, 16, 16);
ctx.fillStyle = "rgb(0,0,255)";
ctx.fillRect(0, 0, 8, 8);
ctx.fillRect(8, 8, 8, 8);

gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, c);

for (var ss = 0; ss < 2; ++ss) {
  debug("");
  debug(ss ? "testing vec4 version" : "testing vec3 version");
  var program = wtu.setupProgram(
      gl, ['vshader' + ss, 'fshader' + ss],
      ['vPosition', 'texCoord0'], [0, 1]);
  gl.useProgram(program);
  var loc = gl.getUniformLocation(program, "divisor");

  for (var ii = 0; ii < 3; ++ii) {
    var denominator = Math.pow(2, ii);
    gl.uniform1f(loc, 1 / denominator);
    wtu.clearAndDrawUnitQuad(gl);
    var size = 16 / denominator;
    for (var yy = 0; yy < 32; yy += size) {
      for (var xx = 0; xx < 32; xx += size) {
        var odd = (xx / size + yy / size) % 2;
        var color = odd ? [0, 255, 0, 255] : [0, 0, 255, 255];
        var msg = "" + xx + ", " + yy + ", " + size + ", " + size + " should be " + (odd ? "green" : "blue");
        wtu.checkCanvasRect(gl, xx, yy, size, size, color, msg);
      }
    }
  }
}
wtu.glErrorShouldBe(gl, gl.NO_ERROR, "Should be no errors.");

var successfullyParsed = true;

</script>
<script src="../../../js/js-test-post.js"></script>

</body>
</html>
